package com.kern.jpademo.service;

import com.kern.jpademo.dao.IAuthorDAO;
import com.kern.jpademo.dao.IBookDAO;
import com.kern.jpademo.entity.XAuthor;
import com.kern.jpademo.entity.XBook;
import com.kern.jpademo.util.CommonUtil;
import com.kern.jpademo.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUtil;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by caoshi at 23:12 2021-07-31
 */
@Service
public class BookService {

    @Autowired
    private IBookDAO bookDAO;

    @Autowired
    private EntityManager entityManager;

    @Autowired
    private IAuthorDAO authorDAO;


    public void testQuery1() {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<XBook> query = builder.createQuery(XBook.class);
        Root<XBook> root = query.from(XBook.class);
        query.select(root);
        List<XBook> resultList = entityManager.createQuery(query).getResultList();
        System.out.println(resultList);
    }


    public void testQuery2() {

    }





    public int add() throws ParseException {
        List<XBook> bookList = new ArrayList<>();

        XAuthor lisi = authorDAO.findByName("李四");
        List<XBook> lisiBooks = new ArrayList<>();

        XBook lisiB1 = new XBook(CommonUtil.getUUID(), "Vue 30天教程", DateTimeUtil.getDateByInput("2018-01-26"), 108.0, lisi.getId());
        XBook lisiB2 = new XBook(CommonUtil.getUUID(), "JavaScript程序设计基础", DateTimeUtil.getDateByInput("2020-08-19"), 200.0, lisi.getId());
        XBook lisiB3 = new XBook(CommonUtil.getUUID(), "HTML开发指南", DateTimeUtil.getDateByInput("2020-11-29"), 80.0, lisi.getId());
        lisiBooks.add(lisiB1);
        lisiBooks.add(lisiB2);
        lisiBooks.add(lisiB3);
        bookList.addAll(lisiBooks);

        /***********************************************************************/
        XAuthor chenm = authorDAO.findByName("陈明");
        List<XBook> chenmBooks = new ArrayList<>();

        XBook chenmB1 = new XBook(CommonUtil.getUUID(), "BI产品设计指南", DateTimeUtil.getDateByInput("2019-10-16"), 98.0, chenm.getId());
        XBook chenmB2 = new XBook(CommonUtil.getUUID(), "产品开发流程纲要", DateTimeUtil.getDateByInput("2021-01-08"), 130.0, chenm.getId());
        chenmBooks.add(chenmB1);
        chenmBooks.add(chenmB2);
        bookList.addAll(chenmBooks);

        /***********************************************************************/
        XAuthor weg = authorDAO.findByName("王二狗");
        List<XBook> wegBooks = new ArrayList<>();
        XBook wb1 = new XBook(CommonUtil.getUUID(), "Java 程序设计教程", DateTimeUtil.getDateByInput("2021-02-17"), 102.0, weg.getId());
        XBook wb2 = new XBook(CommonUtil.getUUID(), "C++程序设计", DateTimeUtil.getDateByInput("2018-04-20"), 96.0, weg.getId());
        XBook wb3 = new XBook(CommonUtil.getUUID(), "GoLang 设计", DateTimeUtil.getDateByInput("2019-10-12"), 180.0, weg.getId());
        XBook wb4 = new XBook(CommonUtil.getUUID(), "Web开发指南", DateTimeUtil.getDateByInput("2019-01-30"), 201.0, weg.getId());

        wegBooks.add(wb1);
        wegBooks.add(wb2);
        wegBooks.add(wb3);
        wegBooks.add(wb4);
        bookList.addAll(wegBooks);

        /***********************************************************************/
        XAuthor zs = authorDAO.findByName("张三");
        List<XBook> zsBooks = new ArrayList<>();
        XBook zsb1 = new XBook(CommonUtil.getUUID(), "系统架构指南", DateTimeUtil.getDateByInput("2018-05-20"), 192.0, zs.getId());

        zsBooks.add(zsb1);
        bookList.addAll(zsBooks);

        bookDAO.saveAll(bookList);

        return 1;

    }





}
